package com.centling.smartSealForPhone.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.adapter.AuditorDeviceAdapter;
import com.centling.smartSealForPhone.bean.AuditorEquipInfoBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Jessie on 2016/8/26.
 */
public class AuditorDeviceActivity extends TitleBarActivity {
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private AutoRecyclerView recyclerView;
    private AuditorDeviceAdapter adapter;
    private GridLayoutManager manager;
    private List<AuditorEquipInfoBean> auditorEquipInfoBeanList = new ArrayList<>();
    private int pageNo = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBarText("设备信息");
        setContentView(R.layout.activity_auditor_device);
        initView();
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh();
            }
        });

    }

    private void initView() {
        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_auditor_device);
        recyclerView = (AutoRecyclerView) findViewById(R.id.rv_auditor_device);
        adapter = new AuditorDeviceAdapter(mContext, auditorEquipInfoBeanList);
        manager = new GridLayoutManager(mContext, 1, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {

                sendRequest(true);
            }
        });

    }


    private void sendRequest(final boolean isRefresh) {
        if (isRefresh) {
            pageNo = 0;
        }
        getEnqueue(UrlConstants.MANAGER_EQUIP_INFO + pageNo, new OkHttpUtil.NetCallBack() {

            @Override
            public void onSucceed(String json) {
                if (isRefresh) {
                    auditorEquipInfoBeanList.clear();
                    ptrClassicFrameLayout.refreshComplete();
                    recyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                                                         @Override
                                                         public void onLoadMore() {
                                                             sendRequest(false);
                                                         }
                                                     }
                    );
                }
                JSONArray data = null;
                try {
                    data = new JSONObject(json).getJSONArray("rows");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                List<AuditorEquipInfoBean> auditorSealSearchListBeanTemp;

                auditorSealSearchListBeanTemp = new Gson().fromJson(data.toString(), new TypeToken<ArrayList<AuditorEquipInfoBean>>() {
                }.getType());
		if (auditorSealSearchListBeanTemp == null) {
		    recyclerView.loadMoreComplete(true);
		    return;
		}
                auditorEquipInfoBeanList.addAll(auditorSealSearchListBeanTemp);
                pageNo++;
                recyclerView.getAdapter().notifyDataSetChanged();

                if (auditorSealSearchListBeanTemp.size() < 20) {
                    recyclerView.loadMoreComplete(true);
                } else {
                    recyclerView.loadMoreComplete(false);
                }
            }

            @Override
            public void onFailed(String reason) {
                ShowToast.shortTime(reason);
            }
        });

    }
}